package c.x.jy.common.mqtt;

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

public class ReadConfig {
	private static final String PROPERTIES_DEFAULT = "mqttConfig.properties";
	private static Map<String, String> VALMAP = new HashMap<String, String>();
	private static Properties properties;

	static {
		init();
	}

	public static String getContent(String key) {

		if (key == null)
			return null;
		String value = null;
		try {
			if (VALMAP.isEmpty() || !VALMAP.containsKey(key)) {

				String val = properties.getProperty(key);
				if (val != null && !val.equals(""))
					VALMAP.put(key, val.trim());

			}
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
		try {
			value = VALMAP.containsKey(key) ? VALMAP.get(key) : null;
		} catch (Exception e) {
			// TODO: handle exception
		}
		return value;
	}

	private static void init() {

		InputStream inputStream = null;
		InputStreamReader strem = null;
		properties = new Properties();
		try {
			inputStream = ReadConfig.class.getClassLoader().getResourceAsStream(PROPERTIES_DEFAULT);
			strem = new InputStreamReader(inputStream);
			properties.load(strem);
			// KEY = properties.getProperty("KEY").trim();
		} catch (Exception e) {
			//e.printStackTrace();
		} finally {
			if (inputStream != null)
				try {
					inputStream.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			if (strem != null) {
				try {
					strem.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}

	}

	public static void main(String[] args) {
		System.out.println( getMqttHttpHost());
	}

	 

	public static String getMqttHost() {
 
		return getContent("mqtt.host");
	}

	public static String getMqttClientrUser() {
 
		return getContent("mqtt.clientusername");
	}

	public static String getMqttClientrPassword() {
 
		return getContent("mqtt.clientpassword");
	}

	public static String getMqttServerClient() {
 
		return getContent("mqtt.clientid");
	}

	public static String getMqttHttpHost() {
 
		return getContent("mqtt.httphost");
	}
	
	
 
}
